var express = require('express');
var router = express.Router();
var utils = require('../../functions/utils');

router.get('/' , function(req,res)
{
	var sessionKey = req.session.key;

	if(utils.isValidString(sessionKey))
	{
		var async = require('async');
		async.waterfall([
			function(cb)
			{
				utils.checkManagerSession(sessionKey , cb);
			}
			,function(result , cb) 
			{
				// 실패 - 로그인 페이지 
				if(result == 0)
				{
					utils.notifyManagerSessionExpired(req , res);
				}
				// 성공 - 실제 작업코드.
				else
				{	
				/////////////////////////////////////////////////////////////////////////////////////////////	

					var fs = require('fs');
					fs.readFile('./template/currentTimeStatistics' , function(error,data)
					{
						if(error)
						{
							throw error;
						}
						var html = String(data);

						// trace log 중 edit 관련항목 사용.
						global.replica_db.connection.getConnection(function(err,connection)
						{
							if(err)
							{
								throw err;
							}
						

							var sql = 'call manage_nowStatistics';

							var query = connection.query(sql, null , function (err, rows) 
							{
								connection.release();
								if(err)
								{
									throw err;
								}

								var content = '';

								html = html.split("%CURRENT_TIME%").join(rows[0][0].current); 

								var row_index=0;

								for(var i = 0 ; i < rows[1].length ; i ++)
								{
									for(var ri = 2 ; ri <= 9 ; ri ++)
									{
										for(var r = 0 ; r < rows[ri].length ; r++)
										{
											// 날짜같은거 모은다.
											if(rows[1][i].day == rows[ri][r].day)
											{
												switch(ri)
												{
													case 2:// current_join: 2
														rows[1][i].current_join = rows[ri][r].current_join;
														break;
													case 3:// day_join: 3
														rows[1][i].day_join = rows[ri][r].day_join;
														break
													case 4:// current_article: 4 
														rows[1][i].current_article = rows[ri][r].current_article;
														break;
													case 5:// day_article: 5
														rows[1][i].day_article = rows[ri][r].day_article;
														break;
													case 6:// current_android: 6
														rows[1][i].current_android = rows[ri][r].current_android;
														break;
													case 7:// day_android: 7
														rows[1][i].day_android = rows[ri][r].day_android;
														break;
													case 8:// current_ios: 8
														rows[1][i].current_ios = rows[ri][r].current_ios;
														break;
													default:// day_ios: 9
														rows[1][i].day_ios = rows[ri][r].day_ios;
												}
											}
										}
									}

									var row = '<tr><td>'+rows[1][i].day + '&nbsp;&nbsp;<font color=blue>';

									switch(rows[1][i].week_day)
									{
										case 1:
											row +='일';
											break;
										case 2:
											row +='월';
											break;
										case 3:
											row +='화';
											break;
										case 4:
											row +='수';
											break;
										case 5:
											row +='목';
											break;
										case 6:
											row +='금';
											break;
										default:
											row +='토';

									}
									row += '</font></td>';

									row += '<td align=right>'+rows[1][i].current_join+'&nbsp;</td>';
									if( i == 0)
										row += '<td></td>';
									else
										row += '<td align=right>'+rows[1][i].day_join+'&nbsp;</td>';

									row += '<td align=right>'+rows[1][i].current_article+'&nbsp;</td>';
									if( i == 0)
										row += '<td></td>';
									else
										row += '<td align=right>'+rows[1][i].day_article+'&nbsp;</td>';

									row += '<td align=right>'+rows[1][i].current_android+'&nbsp;</td>';
									if( i == 0)
										row += '<td></td>';
									else
										row += '<td align=right>'+rows[1][i].day_android+'&nbsp;</td>';

									row += '<td align=right>'+rows[1][i].current_ios+'&nbsp;</td>';
									if( i == 0)
										row += '<td></td>';
									else
										row += '<td align=right>'+rows[1][i].day_ios+'&nbsp;</td></tr>';

									content += row;

									//일주일 간격 자른다.
									if(rows[1][i].week_day == 1)
									{
										content += '<tr bgcolor=black><td colspan=9></td></tr>';
									}

								}

								// total count 
								html = html.split("%TOT_USER_COUNT%").join(rows[10][0].totUserCount); 
								html = html.split("%TOT_ARTICLE_COUNT%").join(rows[11][0].totArticleCount); 
								html = html.split("%TOT_ANDROID_COUNT%").join(rows[12][0].totAndroidCount); 
								html = html.split("%TOT_IOS_COUNT%").join(rows[13][0].totIOSCount); 


								html = html.split("%CONTENT%").join(content); 
								res.writeHead(200 , {'Content-Type' : 'text/html; charset=utf-8'});

								res.end(html);


							}); // query

						}); // getConnection

					}); // readfile
			//////////////////////////////////////////////////////////////////////////////////////////

				} // result==1 
			}
		]); //waterfall
	}
	// 암것도 없으면 loginPage
	else
	{
		utils.gotoManagerLoginPage(req , res);
	}
});

module.exports = router;